WWFHUNTERALLSPECROTATIONPRESETS = false

--===============--
--== FUNCTIONS ==--
--===============--

BurstCD = nil
function BurstCD()
	if (BCD[1][2] == 2 or (BCD[1][2] == 1 and UnitLevel("target") == -1))
	and not UnitDebuff("player",155330) and not UnitDebuff("player",155506)
	and (UnitIsPlayer("target") or (not UnitIsPlayer("target") and UnitHealth("target") > UnitHealthMax("player"))) then
		return true
	else
		return false
	end
end

HunterValidTarget = nil
function HunterValidTarget()
	if UnitAffectingCombat("player") then
		if UnitExists("target") then
			if not UnitIsDeadOrGhost("target") then
				if UnitCanAttack("player","target") and not UnitBuffID("target",155176) and not UnitBuffID("target",176141) then 
					return true
				end
			end
		end
	end
end

function PreSteadyFocusCheck(CobraShotCastingTime,SFT)
	
	if GetSpecialization() == 2 then FS_ID = 163485 -- MM
	elseif GetSpecialization() == 1 or GetSpecialization() == 3 then FS_ID = 152245 -- BM or SV
	else FS_ID = 0
	end
	
	if IsPlayerSpell(FS_ID) then CobraShotCount = 0 return false end
		
	if CobraShotCount > 1 or SFT > (10 - CobraShotCastingTime) then CobraShotCount = 0 end
	
end

--========================--
--== Rotation functions ==--
--========================--

HunterTrackingFunc = nil
function HunterTrackingFunc()
	
	-- Specialization tracking
	if LastHunterSpec ~= GetSpecialization() then WWFHUNTERALLSPECROTATIONPRESETS = false return true end
	
	-- Prevent extra Focusing Shot -- this is not best way, but better than losing time and focus overcap
	if GetSpecialization() == 2 then FS_ID = 163485 -- MM
	elseif GetSpecialization() == 1 or GetSpecialization() == 3 then FS_ID = 152245 -- BM or SV
	else FS_ID = 0
	end
	if UnitCastingInfo("player") == GetSpellInfo(FS_ID) and UnitPower("player",2) > 85 then
		SpellCancelQueuedSpell()
		SpellStopCasting()
		return true
	end
	
	-- Prevent double Dismiss Pet -- same problem like with focus regeneration - slow game interface reaction
	if UnitCastingInfo("player") == GetSpellInfo(2641) and not UnitExists("pet") then
		SpellCancelQueuedSpell()
		SpellStopCasting()
		return true
	end
	
	-- Autocast Aspect of the pack
	if HaveGlyph(119462) and IsMoving() and not UnitBuffID("player",5118) and not UnitBuffID("player",13159) and IsSpellAvailable(5118) and not AggroCheck("player","target") then CastSpellByName(GetSpellInfo(5118),'player') return true end
	
	-- Trap Launcher
	if not UnitBuffID("player",77769) then CastSpellByName(GetSpellInfo(77769),'player') return true end
	
	if ExecuteAoEspell() then return true end
	
	-- T100 talent weapon ench
	if IsPlayerSpell(162534) and not UnitAffectingCombat("player") and not IsMoving() then
		if RV[1][2] == 1 then -- Poison
			if UnitBuffID("player",162537) and UnitCastingInfo("player") == GetSpellInfo(162537) then SpellCancelQueuedSpell() SpellStopCasting() return true end
			if not UnitBuffID("player",162537) then
				CastSpellByName(GetSpellInfo(162537),'player')
				return true
			end
		elseif RV[1][2] == 0 then -- Fire
			if UnitBuffID("player",162541) and UnitCastingInfo("player") == GetSpellInfo(162541) then SpellCancelQueuedSpell() SpellStopCasting() return true end
			if not UnitBuffID("player",162541) then
				CastSpellByName(GetSpellInfo(162541),'player')
				return true
			end
		end
	end
	
	-- Pet management
	if UnitExists("pet") then if UnitIsDead("pet") then MyPet = "dead" else MyPet = "alive" end else MyPet = false end
	
	if not IsPlayerSpell(155228) then -- if "Lone Wolf" talent is not taken
		
-- have a problem - it always summons pet from 1st slot
-- need to do upgrade for addon interface or at least additional macro command to chose another pet slot - may be in future...
		
--		if not UnitExists("pet") then
--			if MyPet == "alive" or not MyPet then
--				CastSpellByName(GetSpellInfo(883))
--				MyPet = "dead"
--			end
--		end
	else
		if UnitExists("pet") then -- Dismiss Pet
			local MyPetID = GetUnitID("pet")
			if MyPetID < 87076 or MyPetID > 87082 then
				CastSpellByName(GetSpellInfo(2641))
				return true
			end
		end
	end
	
end

HunterPause = nil
function HunterPause()
	
	local FoodBuff = false
	for i = 1, #Food do if UnitBuffID("player",Food[i]) then FoodBuff = true end end
	
	local Control = false
	if UnitDebuff("target",19386) or UnitDebuff("target",19503) then Control = true StopAttack() PetStopAttack() end
	
	local FirstAid = UnitBuffID("player",161255)
	
	if IsShiftKeyDown() or IsLeftAltKeyDown() or (IsMounted() and not UnitBuffID("player",164222) and not UnitBuffID("player",165803)) or FirstAid or FoodBuff or Control or UnitBuffID("player",5384) or UnitChannelInfo("player") == GetSpellInfo(120360) then return true end
	
end

HunterBossEvents = nil
function HunterBossEvents()
	for i = 1, 4 do	if UnitExists("boss"..i) then
		
		Mode = InstanceDifficulty()
		BossID = GetUnitID("boss"..i)
		
		if UnitExists("target") then
			TargetHP = UnitHealth("target")*100/UnitHealthMax("target")
			TargetID = GetUnitID("target")
                else	TargetHP = 0	TargetID = 0	end
		
		-- use this over TargetLastTarget() its more reliable
		local name, realm = UnitName("target")
		if not realm then
			if not name then lasttargetname = "none" else lasttargetname = name end
		else
			lasttargetname = name.."-"..realm
		end
		
		AshesCount()
		
	end end
end

--===========================================================--
--========================= SAVES ===========================--
--===========================================================--

HunterSaveAbilities = nil
function HunterSaveAbilities()
	
	local MyHP = UnitHealth("player")*100/UnitHealthMax("player")
        local MyFocus = UnitPower("player",2)
	local Combat = UnitAffectingCombat("player")
	
	-- Healing Tonic
--	local HTcount = GetItemCount(109223,false,true)
--	local HTcd = GetItemCooldown(109223) or 0
        -- HealthStone
--	local HScount = GetItemCount(5512,false,true) or 0
--	local HScd = GetItemCooldown(5512) or 0
--	if Combat then
--		if MyHP < HSHP then
--			if HTcount > 0 then
--				if HTcd == 0 then
--					if not SHBL() then
--						UseItemByName(109223)
--						return true
--					end
--				end
--			elseif HScount > 0 then
--				if HScd == 0 then
--					if not SHBL() then
--						UseItemByName(5512)
--						return true
--					end
--				end
--			end
--		end
--        end
        
        -- Exhilaration
        if IsSpellKnown(109304) then
		if Combat then
			if MyHP < EXHP then
--				if HScount == 0 or HScd ~= 0 then
					if IsSpellAvailable(109304) then
						CastSpellByName(GetSpellInfo(109304))
						return true
					end
--				end
			end
		end
        end
	
	-- Draenei racial
	if IsPlayerSpell(59543) and Combat then
		if not IsSpellKnown(109304) or GetSpellCD(109304) > 5 then
			if MyHP < EXHP then
--				if HScount == 0 or HScd ~= 0 then
					if IsSpellAvailable(59543) then
						CastSpellByName(GetSpellInfo(59543),'player')
						return true
					end
--				end
			end
		end
	end
	
	-- Mend Pet \ Revive Pet
	if MyPet then
		local MyPetID = GetUnitID("pet")
		if MyPetID < 87076 or MyPetID > 87082 then
			local MPe = UnitBuffID("pet",136)
			local PetHP = 100 * ((UnitExists("pet") and UnitHealth("pet")) or 0) / ((UnitExists("pet") and UnitHealthMax("pet")) or 1)
			local GoQR = HaveGlyph(159469) -- Glyph of Quick Revival
			if (UnitExists("pet") and MyPet == "alive" and PetHP <= MPHP and MyFocus >= 35 and not MPe)
			or ((UnitExists("pet") or (not UnitExists("pet") and not IsPlayerSpell(155228))) and MyPet == "dead" and ((not GoQR and MyFocus >= 35 and not IsMoving()) or (GoQR and MyFocus >= 80))) then
				CastSpellByName(GetSpellInfo(982))
				MyPet = false
				return true
			end
		end
	end
	
	-- Special Pet abilities:
	
	-- Spirit Mend
	if BeastMaster and UnitExists("pet") and not UnitIsDead("pet") and UnitPower("pet", 2) >= 5 and GetSpellCD(90361) == 0 then
		hasPetSpells = select(1,HasPetSpells()) or 0
		petType = select(2,HasPetSpells()) or "NO"
		petIcon = GetPetIcon() or "Icon"
		if petType == "PET" and hasPetSpells > 0 and GetPetActionsUsable() then
			for i = 1, 10 do
				if GetPetActionInfo(i) == GetSpellInfo(90361) and GetPetActionSlotUsable(i) and MyHP < SMHP then
					CastSpellByName(GetSpellInfo(90361),'player')
					return true
				end
			end
		end
	end
	
	-- Roar of Sacrifice
	if UnitExists("pet") and not UnitIsDead("pet") and GetSpellCD(53480) == 0 and Combat then
		hasPetSpells = select(1,HasPetSpells()) or 0
		petType = select(2,HasPetSpells()) or "NO"
		if petType == "PET" and hasPetSpells > 0 and GetPetActionsUsable() then
			for i = 1, 10 do
				if GetPetActionInfo(i) == GetSpellInfo(53480) and GetPetActionSlotUsable(i) and MyHP < ROSHP then
					CastSpellByName(GetSpellInfo(53480),'player')
					return true
				end
			end
		end
	end
	
end

--====================================================================--
--========================= TOGGLED SPELLS ===========================--
--====================================================================--

function HunterToggled_Spells()
	
	local MyFocus = UnitPower("player",2)
	local HaVT = HunterValidTarget()
	
	-- Intimidation
	if VFQ[Intimidation_Index][5] == 1 then HUNTERATOWU("CD", 19577, 30, Intimidation_Index) end
	if VFQ[Intimidation_Index][2] == 1 then
		if IsSpellAvailable(19577) then
			if HaVT then
				if PetInRange("target") then
					CastSpellByName(GetSpellInfo(19577),'target')
					return true
				end
			end
		end
	end
	
	-- Wyvern Sting
	if VFQ[WyvernSting_Index][5] == 1 then HUNTERATOWU("CD", 19386, 15, WyvernSting_Index) end
	if VFQ[WyvernSting_Index][2] == 1 then
		if IsSpellAvailable(19386) then
			if UnitExists("focus") then
				if not UnitIsDeadOrGhost("focus") then
					if UnitCanAttack("player","focus") then
						CastSpellByName(GetSpellInfo(19386),'focus')
						return true
					end
				end
			elseif HaVT then
				CastSpellByName(GetSpellInfo(19386),'target')
				StopAttack()
				PetStopAttack()
				Sleep_Time = 2
				return true
			end
		end
	end
	
	-- Binding Shot
	if VFQ[BindingShot_Index][5] == 1 then HUNTERATOWU("CD", 109248, 15, BindingShot_Index) end
	if VFQ[BindingShot_Index][2] == 1 then
		if IsSpellAvailable(109248) then
			CastSpellByName(GetSpellInfo(109248))
			return true
		end
	end
	
	-- Revive Pet (Forced)
	if VFQ[RevivePet_Index][5] == 1 then HUNTERATOWU("RevivePet", 982, 0, RevivePet_Index) end
	if VFQ[RevivePet_Index][2] == 1 then
		if IsSpellAvailable(982) then
			if MyPet == "dead" then
				if (not HaveGlyph(159469) and not IsMoving()) or HaveGlyph(159469) then
					CastSpellByName(GetSpellInfo(982))
					return true
				end
			end
		end
	end
	
	-- Concussive Shot
	if VFQ[ConcussiveShot_Index][5] == 1 then HUNTERATOWU("CD", 5116, 2, ConcussiveShot_Index) end
	if VFQ[ConcussiveShot_Index][2] == 1 then
		if HaVT then
			if IsSpellAvailable(5116) and UnitLevel("target") > 0 then
				CastSpellByName(GetSpellInfo(5116),'target')
				return true
			end
		end
	end
	
	-- Distracting Shot
	if VFQ[DistractingShot_Index][5] == 1 then HUNTERATOWU("CD", 20736, 2, DistractingShot_Index) end
	if VFQ[DistractingShot_Index][2] == 1 then
		if HaVT then
			if IsSpellAvailable(20736) then
				CastSpellByName(GetSpellInfo(20736),'target')
				return true
			end
		end
	end
	
	-- Explosive Trap
	if VFQ[ExplosiveTrap_Index][5] == 1 then HUNTERATOWU("CD", 13813, 2, ExplosiveTrap_Index) end
	if VFQ[ExplosiveTrap_Index][2] == 1 then
		if IsSpellAvailable(13813) then
			RunMacroText('/cast '..GetSpellInfo(13813))
			return true
		end
	end
	
	-- Freezing Trap
	if VFQ[FreezingTrap_Index][5] == 1 then HUNTERATOWU("CD", 1499, 2, FreezingTrap_Index) end
	if VFQ[FreezingTrap_Index][2] == 1 then
		if IsSpellAvailable(1499) then
			RunMacroText('/cast '..GetSpellInfo(1499))
			return true
		end
	end
	
	-- Ice Trap
	if VFQ[IceTrap_Index][5] == 1 then HUNTERATOWU("CD", 13809, 2, IceTrap_Index) end
	if VFQ[IceTrap_Index][2] == 1 then
		if IsSpellAvailable(13809) then
			RunMacroText('/cast '..GetSpellInfo(13809))
			return true
		end
	end
	
	-- Snake Trap
	if VFQ[SnakeTrap_Index][5] == 1 then HUNTERATOWU("CD", 34600, 2, SnakeTrap_Index) end
	if VFQ[SnakeTrap_Index][2] == 1 then
		if IsSpellAvailable(34600) then
			RunMacroText('/cast '..GetSpellInfo(34600))
			return true
		end
	end
	
	-- Feign Death
	if VFQ[FeignDeath_Index][5] == 1 then HUNTERATOWU("CD", 5384, 15, FeignDeath_Index) end
	if VFQ[FeignDeath_Index][2] == 1 then
		if IsSpellAvailable(5384) then
			CastSpellByName(GetSpellInfo(5384))
			StopAttack()
			Sleep_Time = 2
			return true
		end
	end
	
end

BMRotation = nil
function BMRotation()
	
	if HunterValidTarget() and IsSpellInRange(GetSpellInfo(3044),"target") == 1 then
	
	local MyFocus = UnitPower("player",2)
	local TargetID = GetUnitID("target")
	local TargetHP = UnitHealth("target")*100/UnitHealthMax("target")
	local _, activeRegen = GetPowerRegen()
	local CobraShotCastingTime = select(4,GetSpellInfo(56641))/1000
	
	if IsPlayerSpell(177667) then -- Steady Focus
		SF,_,_,_,_,_,SFT = UnitBuffID("player",177668)		-- Steady Focus Buff
		if SF then SFT = SFT - GetTime() else SFT = 0 end	-- Steady Focus Buff Timer
		PreSteadyFocusCheck(CobraShotCastingTime,SFT)
	else
		SF = 0
		SFT = 0
	end
	
	if UnitExists("pet") and not UnitIsDead("pet") then
		FF,_,_,FFC,_,_,FFT = UnitBuff("pet",GetSpellInfo(19623)) -- Focus Fire / Frenzy
	else
		FF = nil; FFC = 0; FFT = 0;
	end
	
	local BW,_,_,_,_,_,BWT = UnitBuffID("player",19574) -- Bestial Wrath
	local Potion,_,_,_,_,_,PT = UnitBuffID("player",156423) -- Draenor Agility Potion
	local TR,_,_,_,_,_,TRT = UnitBuffID("player",26297) -- Troll Racial
	local OR,_,_,_,_,_,ORT = UnitBuffID("player",33697) -- Orc Racial
	local FocusFire,_,_,_,_,_,FFTimer = UnitBuffID("player",82692) -- Focus Fire
	local BC = UnitBuffID("pet",115939) -- Beast Cleave
	local TotH = UnitBuffID("player",34720) -- Thrill of the Hunt
	
	local SS = UnitDebuffID("target",118253) -- Serpent Sting
	
	if FF then FFC = FFC FFT = FFT - GetTime() else FFC = 0 FFT = 0 end -- Pet Frenzy Buff Timer
	if BW then BWT = BWT - GetTime() else BWT = 0 end -- Bestial Wrath Buff Timer
	if Potion then PT = PT - GetTime() else PT = 0 end -- Potion Buff Timer
	if TR then TRT = TRT - GetTime() else TRT = 0 end -- Troll Racial Buff Timer
	if OR then ORT = ORT - GetTime() else ORT = 0 end -- Orc Racial Buff Timer
	if FocusFire then FFTimer = FFTimer - GetTime() else FFTimer = 0 end -- Focus Fire Buff Timer
	
	local TBT1, TBT2 = CTT() -- Trinkets Buff Timers
	local BLT = CBLT() -- Bludlust timer
	
	if TotH then ExtraFocus = 20 else ExtraFocus = 0 end
	
	EnemyCount = GetEnemyCount()
	
	InDiff = InstanceDifficulty()
	
	-- Mirror of the Blademaster trinket
	local Trinket1 = select(1,GetInventoryItemID("player", 13))
	local Trinket2 = select(1,GetInventoryItemID("player", 14))
	if Trinket1 == 124224 or Trinket2 == 124224 then Blademaster = true else Blademaster = false end
	
	if InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and (GetUnitID("boss1") ~= 90199 or (GetUnitID("boss1") == 90199 and (UnitDebuffID("player",181295) or UnitChannelInfo("boss1") == GetSpellInfo(181973))))) then
		Gorefiend = true
	else
		Gorefiend = false
	end
	
--	if UnitExists("pet") then if not UnitIsDead("pet") then PetAttack("target") end end
	
	-- Tranquilizing Shot
	if IsSpellAvailable(19801) and VFQ[TranqShot_Index][2] == 1 then
		if UnitExists("focus") and not UnitIsDeadOrGhost("focus") and UnitCanAttack("player","focus") and IsSpellInRange(GetSpellInfo(19801), "focus") == 1 then
			if (HaveGlyph(119384) or MyFocus >= 20) and TranquilizingShot("focus") then
				CastSpellByName(GetSpellInfo(19801),'focus')
				return true
			end
		else
			if (HaveGlyph(119384) or MyFocus >= 20) and TranquilizingShot("target") then
				CastSpellByName(GetSpellInfo(19801),'target')
				return true
			end
		end
	end
	
	-- Blood Elf Racial
	if IsPlayerSpell(80483) and BurstCD() and MyFocus <= 30 then
		if IsSpellAvailable(80483) then
			CastSpellByName(GetSpellInfo(80483),'player')
			return true
		end
	end
	-- Orc Racial
	if IsPlayerSpell(33697) and BurstCD() then
		if IsSpellAvailable(33697) then
			CastSpellByName(GetSpellInfo(33697),'player')
			return true
		end
	end
	-- Troll Racial
	if IsPlayerSpell(26297) and BurstCD() then
		if IsSpellAvailable(26297) then
			CastSpellByName(GetSpellInfo(26297),'player')
			return true
		end
	end
	
-- actions+=/stampede,if=buff.bloodlust.up|buff.focus_fire.up|target.time_to_die<=25
	
	if IsPlayerSpell(121818) then
		if IsSpellAvailable(121818) and BurstCD() and (BLT > 30 or FFTimer > 15 or PT > 20 or BW or (UnitLevel("target") == -1 and TargetHP < 15)) then
			CastSpellByName(GetSpellInfo(121818),'target')
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			return true
		end
	end
	
-- actions+=/dire_beast
	
	if IsPlayerSpell(120679) then
		if IsSpellAvailable(120679) and MyFocus < 110 and (UnitLevel("target") == -1 or UnitHealth("target") > UnitHealthMax("player")) then
			CastSpellByName(GetSpellInfo(120679),'target')
			return true
		end
	end
	
-- actions+=/focus_fire,if=buff.focus_fire.down&((cooldown.bestial_wrath.remains<1&buff.bestial_wrath.down)|(talent.stampede.enabled&buff.stampede.remains)|pet.cat.buff.frenzy.remains<1)
	
	if IsSpellAvailable(82692) and not FocusFire and FFC >= 1 then
		if ((BurstCD() and (GetSpellCD(19574) < 1 or BW)) or (GetSpellCD(121818) >= 270 and GetSpellCD(121818) <= 300) or (FFT > 0 and FFT < 2)) then
			CastSpellByName(GetSpellInfo(82692))
			return true
		end
	end
	
	-- Use Mirror of the Blademaster trinket
	if BurstCD() and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend and BW then
		local Trinket1 = select(1,GetInventoryItemID("player", 13))
		local Trinket2 = select(1,GetInventoryItemID("player", 14))
		if Trinket1 == 124224 then UseInventoryItem(13)
		elseif Trinket2 == 124224 then UseInventoryItem(14) end
	end
	
-- actions+=/bestial_wrath,if=focus>30&!buff.bestial_wrath.up
	
	if IsSpellAvailable(19574) and BurstCD() and UnitExists("pet") and MyFocus > 30 and not BW then
		if not UnitIsDead("pet") then
			CastSpellByName(GetSpellInfo(19574))
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			return true
		end
	end
	
-- actions+=/multishot,if=spell_targets.multi_shot>1&pet.cat.buff.beast_cleave.remains<0.5
	
	-- Cleave part for number of targets > 1 (AoE Rotation must be activated)
	if RV[1][2] == 0 and Gorefiend then
		
		-- maintain beast cleave buff on your pet
		if IsSpellAvailable(2643) and EnemyCount > 1 and not BC and not UnitIsDead("pet") and PetInRange("target") and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) then
			CastSpellByName(GetSpellInfo(2643),'target')
			return true
		end
		
		-- Focus Fire
		if IsSpellAvailable(82692) and not FocusFire and FFC == 5 then
			CastSpellByName(GetSpellInfo(82692))
			return true
		end
		
		-- Barrage > 1 enemy
		if IsPlayerSpell(120360) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) then -- Barrage
			if IsSpellAvailable(120360) and EnemyCount > 1 and (BC or UnitIsDead("pet") or (not UnitIsDead("pet") and not PetInRange("target"))) then
				CastSpellByName(GetSpellInfo(120360),'target')
				return true
			end
		end
		
		-- maximum AoE
		if IsSpellAvailable(2643) and EnemyCount > 5
		and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343))
		and (not IsPlayerSpell(120360) or (IsPlayerSpell(120360) and GetSpellCD(120360) > 5)) then
			CastSpellByName(GetSpellInfo(2643),'target')
			return true
		end
		
	end
	
-- actions+=/focus_fire,min_frenzy=5
	
	if IsSpellAvailable(82692) and not FocusFire and FFC == 5 then
		CastSpellByName(GetSpellInfo(82692))
		return true
	end
	
-- actions+=/explosive_trap,if=spell_targets.explosive_trap_tick>5
	
-- actions+=/kill_command
	
	if IsSpellAvailable(34026) and RV[1][2] == 1 and UnitExists("pet") and not UnitIsDead("pet") and PetInRange("target") then
		CastSpellByName(GetSpellInfo(34026),'target')
		return true
	end
	
-- actions+=/a_murder_of_crows
	
	if IsPlayerSpell(131894) then
		if IsSpellAvailable(131894) and BurstCD() and (UnitLevel("target") == -1 or UnitHealth("target") > UnitHealthMax("player")) then
			CastSpellByName(GetSpellInfo(131894),'target')
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			return true
		end
	end
	
-- actions+=/kill_shot,if=focus.time_to_max>gcd
	
	if RV[1][2] == 1 then
		if GetSpellCD(53351) <= 0.5 and (TargetHP < 20 or (TargetID == 79015 and KoraghSP() > 0 and KoraghSP() < 20)) and MyFocus < 100 then 
			CastSpellByName(GetSpellInfo(53351),'target')
			return true
		end
	end
	
-- actions+=/focusing_shot,if=focus<50
	
	if IsSpellAvailable(56641) and IsPlayerSpell(152245) and MyFocus < 50 and not IsMoving() then
		CastSpellByName(GetSpellInfo(56641),'target')
		return true
	end
	
-- actions+=/cobra_shot,if=buff.pre_steady_focus.up&buff.steady_focus.remains<7&(14+cast_regen)<focus.deficit
-- actions+=/cobra_shot,if=talent.steady_focus.enabled&buff.steady_focus.remains<4&focus<50
	
	if IsSpellAvailable(56641) and not IsPlayerSpell(152245) and CobraShotCount == 1
	and (SFT < 7 or (SFT < 4 and MyFocus < 50))
	and (14 + activeRegen*CobraShotCastingTime) < (UnitPowerMax("player",2) - MyFocus) then
		CastSpellByName(GetSpellInfo(56641),'target')
		return true
	end
	
-- actions+=/explosive_trap,if=spell_targets.explosive_trap_tick>1
	
-- actions+=/glaive_toss
	
	if IsPlayerSpell(117050) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(117050) then
			CastSpellByName(GetSpellInfo(117050),'target')
			return true
		end
	end
	
-- actions+=/barrage
	
	if IsPlayerSpell(120360) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(120360) and EnemyCount > 1 then
			CastSpellByName(GetSpellInfo(120360),'target')
			return true
		end
	end
	
-- actions+=/powershot,if=focus.time_to_max>cast_time
	
	if IsPlayerSpell(109259) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		local PowerShotCastingTime = select(4,GetSpellInfo(109259))/1000
		if IsSpellAvailable(109259) and (UnitPowerMax("player",2) - MyFocus)/activeRegen > PowerShotCastingTime then
			CastSpellByName(GetSpellInfo(109259),'target')
			return true
		end
	end
	
-- actions+=/cobra_shot,if=spell_targets.multi_shot>5
	
	if IsSpellAvailable(56641) and not IsPlayerSpell(152245) and EnemyCount > 5 and RV[1][2] == 0 then
		CastSpellByName(GetSpellInfo(56641),'target')
		return true
	end
	
-- actions+=/arcane_shot,if=(buff.thrill_of_the_hunt.react&focus>35)|buff.bestial_wrath.up
	
	if IsSpellAvailable(3044) and ((TotH and MyFocus > 35) or BW) then
		CastSpellByName(GetSpellInfo(3044),'target')
		return true
	end
	
-- actions+=/arcane_shot,if=focus>=75
	
	if IsSpellAvailable(3044) and MyFocus >= 75 then
		CastSpellByName(GetSpellInfo(3044),'target')
		return true
	end
	
	-- Focus regeneration
	if IsPlayerSpell(152245) then
		
-- Focusing Shot
		
		if IsSpellAvailable(152245) and not IsMoving() then
			CastSpellByName(GetSpellInfo(152245),'target')
			return true
		end
	else
		
-- actions+=/cobra_shot
		
		if IsSpellAvailable(56641) then 
			CastSpellByName(GetSpellInfo(56641),'target')
			return true
		end
	end
	
	end
	
end

MMRotation = nil
function MMRotation()
	
	if HunterValidTarget() and IsSpellInRange(GetSpellInfo(53209), "target") == 1 then
	
	local MyFocus = UnitPower("player",2) 
	local TargetID = GetUnitID("target")
	local TargetHP = UnitHealth("target")*100/UnitHealthMax("target")
	local _, activeRegen = GetPowerRegen()
	local SteadyShotCastingTime = select(4,GetSpellInfo(56641))/1000
	local AimedShotCastingTime = select(4,GetSpellInfo(19434))/1000
	
	if IsPlayerSpell(177667) then -- Steady Focus
		SF,_,_,_,_,_,SFT = UnitBuffID("player",177668)		-- Steady Focus Buff
		if SF then SFT = SFT - GetTime() else SFT = 0 end	-- Steady Focus Buff Timer
		PreSteadyFocusCheck(CobraShotCastingTime,SFT)
	end
	
	local Potion,_,_,_,_,_,PT = UnitBuffID("player",156423) -- Draenor Agility Potion
	local RF,_,_,_,_,_,RFT = UnitBuffID("player",3045) -- Rapid Fire
	local TR,_,_,_,_,_,TRT = UnitBuffID("player",26297) -- Troll Racial
	local OR,_,_,_,_,_,ORT = UnitBuffID("player",33697) -- Orc Racial
	local Bomb = UnitBuffID("player",35110) -- Bombardment
	local TotH = UnitBuffID("player",34720) -- Thrill of the Hunt
	
	if Potion then PT = PT - GetTime() else PT = 0 end -- Potion Buff Timer
	if RF then RFT = RFT - GetTime() else RFT = 0 end -- Rapid Fire Buff Timer
	if TR then TRT = TRT - GetTime() else TRT = 0 end -- Troll Racial Buff Timer
	if OR then ORT = ORT - GetTime() else ORT = 0 end -- Orc Racial Buff Timer
	
	local TBT1, TBT2 = CTT() -- Trinkets Buff Timers
	local BLT = CBLT() -- Bludlust timer
	
	if TotH then ExtraFocus = 20 else ExtraFocus = 0 end
	
	EnemyCount = GetEnemyCount()
	
	InDiff = InstanceDifficulty()
	
	-- Mirror of the Blademaster trinket
	local Trinket1 = select(1,GetInventoryItemID("player", 13))
	local Trinket2 = select(1,GetInventoryItemID("player", 14))
	if Trinket1 == 124224 or Trinket2 == 124224 then Blademaster = true else Blademaster = false end
	
	if InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and (GetUnitID("boss1") ~= 90199 or (GetUnitID("boss1") == 90199 and (UnitDebuffID("player",181295) or UnitChannelInfo("boss1") == GetSpellInfo(181973))))) then
		Gorefiend = true
	else
		Gorefiend = false
	end
	
	-- Tranquilizing Shot
	
	if IsSpellAvailable(19801) and VFQ[TranqShot_Index][2] == 1 then
		if UnitExists("focus") and not UnitIsDeadOrGhost("focus") and UnitCanAttack("player","focus") and IsSpellInRange(GetSpellInfo(19801), "focus") == 1 then
			if (HaveGlyph(119384) or MyFocus >= 20) and TranquilizingShot("focus") then
				CastSpellByName(GetSpellInfo(19801),'focus')
				return true
			end
		else
			if (HaveGlyph(119384) or MyFocus >= 20) and TranquilizingShot("target") then
				CastSpellByName(GetSpellInfo(19801),'target')
				return true
			end
		end
	end
	
	-- Blood Elf Racial
	
	if IsPlayerSpell(80483) and BurstCD() and MyFocus <= 30 then
		if IsSpellAvailable(80483) then
			CastSpellByName(GetSpellInfo(80483),'player')
			return true
		end
	end
	
	-- Orc Racial
	
	if IsPlayerSpell(33697) and BurstCD() then
		if IsSpellAvailable(33697) then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(33697),'player')
			return true
		end
	end
	
	-- Troll Racial
	
	if IsPlayerSpell(26297) and BurstCD() then
		if IsSpellAvailable(26297) then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(26297),'player')
			return true
		end
	end
	
	-- Use Mirror of the Blademaster trinket
	if BurstCD() and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		local Trinket1 = select(1,GetInventoryItemID("player", 13))
		local Trinket2 = select(1,GetInventoryItemID("player", 14))
		if Trinket1 == 124224 then UseInventoryItem(13)
		elseif Trinket2 == 124224 then UseInventoryItem(14) end
	end
	
-- actions+=/chimaera_shot
	
	if IsSpellAvailable(53209) and Gorefiend then
		CastSpellByName(GetSpellInfo(53209),'target')
		return true
	end
	
-- actions+=/kill_shot
	
	if not IsPlayerSpell(157707) then
		-- Standart Kill Shot
		if IsSpellAvailable(53351) and GetSpellCD(53351) < 1 and TargetHP <= 20
		and ( (RV[1][2] == 1 and MyFocus < 100) or (RV[1][2] == 0 and MyFocus + ExtraFocus < 40) ) then
			CastSpellByName(GetSpellInfo(53351),'target')
			return true
		end
	else	-- Enchanted Kill Shot
		if IsSpellAvailable(157707) and GetSpellCD(157708) < 1
		and (TargetHP <= 35 or (TargetID == 79015 and KoraghSP() > 0 and KoraghSP() < 35))
		and ( (RV[1][2] == 1 and MyFocus < 100) or (RV[1][2] == 0 and MyFocus + ExtraFocus < 40) ) then
			CastSpellByName(GetSpellInfo(157708),'target')
			return true
		end
	end
	
-- actions+=/rapid_fire
	
	if IsSpellAvailable(3045) and BurstCD() then
		if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
		CastSpellByName(GetSpellInfo(3045),'player')
		return true
	end
	
-- actions+=/stampede,if=buff.rapid_fire.up|buff.bloodlust.up|target.time_to_die<=25
	
	if IsPlayerSpell(121818) then
		if IsSpellAvailable(121818) and BurstCD() and (RF or Potion or BLT >= 20 or TBT1 >= 9 or TBT2 >= 9 or (UnitLevel("target") == -1 and TargetHP < 15)) then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(121818),'target')
			return true
		end
	end
	
-- actions+=/call_action_list,name=careful_aim,if=buff.careful_aim.up
	
	if TargetHP > 80 or (TargetID == 79015 and KoraghSP() > 80) or RF then
		
-- actions.careful_aim=glaive_toss,if=active_enemies>2
		
		if IsPlayerSpell(117050) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
			if IsSpellAvailable(117050) and EnemyCount > 2 then
				CastSpellByName(GetSpellInfo(117050),'target')
				return true
			end
		end
		
-- actions.careful_aim+=/powershot,if=spell_targets.powershot>1&cast_regen<focus.deficit
		
		if IsPlayerSpell(109259) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) then
			local PowerShotCastingTime = select(4,GetSpellInfo(109259))/1000
			if IsSpellAvailable(109259) and EnemyCount > 1 and PowerShotCastingTime*activeRegen < (UnitPowerMax("player",2) - MyFocus) and Gorefiend then
				CastSpellByName(GetSpellInfo(109259),'target')
				return true
			end
		end
		
-- actions.careful_aim+=/barrage,if=spell_targets.barrage>1
		
		if IsPlayerSpell(120360) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
			if IsSpellAvailable(120360) and EnemyCount > 1 then
				CastSpellByName(GetSpellInfo(120360),'target')
				return true
			end
		end
		
		-- Multi-Shot (if you need AoE)
		if IsSpellAvailable(2643) and RV[1][2] == 0 and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
			CastSpellByName(GetSpellInfo(2643),'target')
			return true
		end
		
-- actions.careful_aim+=/aimed_shot
		
		if IsSpellAvailable(19434) then
			CastSpellByName(GetSpellInfo(19434),'target')
			return true
		end
		
		-- Focus Regeneration
		
		if IsPlayerSpell(163485) then
			
-- actions.careful_aim+=/focusing_shot,if=50+cast_regen<focus.deficit
			
			local FocusingShotCastingTime = select(4,GetSpellInfo(163485))/1000
			if IsSpellAvailable(163485) and (50+(FocusingShotCastingTime*activeRegen)) < (UnitPowerMax("player",2) - MyFocus) and not IsMoving() then
				CastSpellByName(GetSpellInfo(163485),'target')
				return true
			end
		else
			
-- actions.careful_aim+=/steady_shot
			
			if IsSpellAvailable(56641) then
				CastSpellByName(GetSpellInfo(56641),'target')
				return true
			end
		end
		
	end
	
-- actions+=/explosive_trap,if=spell_targets.explosive_trap_tick>1
-- can not to do this
	
-- actions+=/a_murder_of_crows
	
	if IsPlayerSpell(131894) then
		if IsSpellAvailable(131894) and BurstCD() then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(131894),'target')
			return true
		end
	end
	
-- actions+=/dire_beast,if=cast_regen+action.aimed_shot.cast_regen<focus.deficit
	
	if IsPlayerSpell(120679) then
		if IsSpellAvailable(120679) and MyFocus <= 80 then
			CastSpellByName(GetSpellInfo(120679),'target')
			return true
		end
	end
	
-- actions+=/glaive_toss
	
	if IsPlayerSpell(117050) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) then
		if IsSpellAvailable(117050) and Gorefiend then
			CastSpellByName(GetSpellInfo(117050),'target')
			return true
		end
	end
	
--actions+=/powershot,if=cast_regen<focus.deficit
	
	if IsPlayerSpell(109259) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) then
		local PowerShotCastingTime = select(4,GetSpellInfo(109259))/1000
		if IsSpellAvailable(109259) and PowerShotCastingTime*activeRegen < (UnitPowerMax("player",2) - MyFocus) and Gorefiend then
			CastSpellByName(GetSpellInfo(109259),'target')
			return true
		end
	end
	
-- actions+=/barrage
	
	if IsPlayerSpell(120360) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(120360) then
			CastSpellByName(GetSpellInfo(120360),'target')
			return true
		end
	end
	
	-- maintain focus reserve for burst
	
-- actions+=/focusing_shot,if=focus.deficit*cast_time%(50+cast_regen)>cooldown.rapid_fire.remains&focus<100
	
	if IsPlayerSpell(163485) then
		local FocusingShotCastingTime = select(4,GetSpellInfo(163485))/1000
		if IsSpellAvailable(163485) and BurstCD() and ((UnitPowerMax("player",2) - MyFocus)*(FocusingShotCastingTime/(50+(FocusingShotCastingTime*activeRegen)))) > GetSpellCD(3045) and MyFocus < 100 and not IsMoving() then
			CastSpellByName(GetSpellInfo(163485),'target')
			return true
		end
	else
		
-- actions+=/steady_shot,if=focus.deficit*cast_time%(14+cast_regen)>cooldown.rapid_fire.remains
		
		if IsSpellAvailable(56641) and BurstCD() and ((UnitPowerMax("player",2) - MyFocus)*(SteadyShotCastingTime/(14+(SteadyShotCastingTime*activeRegen)))) > GetSpellCD(3045) then
			CastSpellByName(GetSpellInfo(56641),'target')
			return true
		end
	end
		
	-- maintain Steady Focus buff
	
-- actions+=/steady_shot,if=buff.pre_steady_focus.up&(14+cast_regen+action.aimed_shot.cast_regen)<=focus.deficit
	
	if IsSpellAvailable(56641) and not IsPlayerSpell(163485) and CobraShotCount == 1
	and (14 + (SteadyShotCastingTime*activeRegen) + (AimedShotCastingTime*activeRegen)) <= (UnitPowerMax("player",2) - MyFocus) then
		CastSpellByName(GetSpellInfo(56641),'target')
		return true
	end
	
-- actions+=/multishot,if=spell_targets.multi_shot>6
-- full automated rotation is not good, switch to AoE rotation manually when needed

	if IsSpellAvailable(2643) and RV[1][2] == 0 and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		CastSpellByName(GetSpellInfo(2643),'target')
		return true
	end
	
-- actions+=/aimed_shot,if=talent.focusing_shot.enabled
-- actions+=/aimed_shot,if=focus+cast_regen>=85
-- actions+=/aimed_shot,if=buff.thrill_of_the_hunt.react&focus+cast_regen>=65
-- 3 in 1
	
	if IsSpellAvailable(19434) and (IsPlayerSpell(163485) or ((MyFocus + (AimedShotCastingTime*activeRegen)) >= 85) or (TotH and (MyFocus + (AimedShotCastingTime*activeRegen)) >= 65) ) then
		CastSpellByName(GetSpellInfo(19434),'target')
		return true
	end
	
	-- Focus Regeneration
	
	if IsPlayerSpell(163485) then
		
-- actions+=/focusing_shot,if=50+cast_regen-10<focus.deficit
		
		local FocusingShotCastingTime = select(4,GetSpellInfo(163485))/1000
		if IsSpellAvailable(163485) and (50+(FocusingShotCastingTime*activeRegen)-10) < (UnitPowerMax("player",2) - MyFocus) and not IsMoving() then
			CastSpellByName(GetSpellInfo(163485),'target')
			return true
		end
	else
		
-- actions+=/steady_shot
		
		if IsSpellAvailable(56641) then
			CastSpellByName(GetSpellInfo(56641),'target')
			return true
		end
	end
	
	end
	
end

SVRotation = nil
function SVRotation()
	
	if HunterValidTarget() and IsSpellInRange(GetSpellInfo(53301), "target") == 1 then
	
	local MyFocus = UnitPower("player", 2)
	local TargetID = GetUnitID("target")
	local TargetHP = UnitHealth("target")*100/UnitHealthMax("target")
	local _, activeRegen = GetPowerRegen()
	local CobraShotCastingTime = select(4,GetSpellInfo(56641))/1000
	
	if IsPlayerSpell(177667) then -- Steady Focus
		SF,_,_,_,_,_,SFT = UnitBuffID("player",177668)		-- Steady Focus Buff
		if SF then SFT = SFT - GetTime() else SFT = 0 end	-- Steady Focus Buff Timer
		PreSteadyFocusCheck(CobraShotCastingTime,SFT)
	end
	
	local Ring,_,_,_,_,_,RingT = UnitBuffID("player",177161) -- Legendary Ring Proc
	local Potion,_,_,_,_,_,PT = UnitBuffID("player",156423) -- Draenor Agility Potion
	local TR,_,_,_,_,_,TRT = UnitBuffID("player",26297) -- Troll Racial
	local OR,_,_,_,_,_,ORT = UnitBuffID("player",33697) -- Orc Racial
	local TotH = UnitBuffID("player",34720) -- Thrill of the Hunt
	
	local SS,_,_,_,_,_,SST = UnitDebuffID("target",118253,"player") -- Serpent Sting
	local FSS,_,_,_,_,_,FSST = UnitDebuffID("focus",118253,"player") -- Serpent Sting "focus"
	local BA,_,_,_,_,_,BAT = UnitDebuffID("target",3674,"player") -- Black Arrow
	local ES = UnitDebuffID("target",53301,"player") -- Explosive Shot
	
	if SS then SST = SST - GetTime() else SST = 0 end -- Serpent Sting Debuff Timer
	if FSS then FSST = FSST - GetTime() else FSST = 0 end -- Serpent Sting "focus" Debuff Timer
	if BA then BAT = BAT - GetTime() else BAT = 0 end -- Black Arrow Debuff Timer
	if Potion then PT = PT - GetTime() else PT = 0 end -- Potion Buff Timer
	if TR then TRT = TRT - GetTime() else TRT = 0 end -- Troll Racial Buff Timer
	if OR then ORT = ORT - GetTime() else ORT = 0 end -- Orc Racial Buff Timer
	if Ring then RingT = RingT - GetTime() else RingT = 0 end -- Orc Racial Buff Timer
	
	if TotH then ExtraFocus = 20 else ExtraFocus = 0 end
	local TBT1, TBT2 = CTT() -- Trinkets Buff Timers
	local BLT = CBLT() -- Bludlust timer
	
	EnemyCount = GetEnemyCount()
	
	InDiff = InstanceDifficulty()
	
	-- Mirror of the Blademaster trinket
	local Trinket1 = select(1,GetInventoryItemID("player", 13))
	local Trinket2 = select(1,GetInventoryItemID("player", 14))
	if Trinket1 == 124224 or Trinket2 == 124224 then Blademaster = true else Blademaster = false end
	
	if InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and (GetUnitID("boss1") ~= 90199 or (GetUnitID("boss1") == 90199 and (UnitDebuffID("player",181295) or UnitChannelInfo("boss1") == GetSpellInfo(181973))))) then
		Gorefiend = true
	else
		Gorefiend = false
	end
	
	-- Tranquilizing Shot
	if IsSpellAvailable(19801) and VFQ[TranqShot_Index][2] == 1 then
		if UnitExists("focus") and not UnitIsDeadOrGhost("focus") and UnitCanAttack("player","focus") and IsSpellInRange(GetSpellInfo(19801), "focus") == 1 then
			if (HaveGlyph(119384) or MyFocus >= 20) and TranquilizingShot("focus") then
				CastSpellByName(GetSpellInfo(19801),'focus')
				return true
			end
		else
			if (HaveGlyph(119384) or MyFocus >= 20) and TranquilizingShot("target") then
				CastSpellByName(GetSpellInfo(19801),'target')
				return true
			end
		end
	end
	
	-- Blood Elf Racial
	if IsPlayerSpell(80483) and BurstCD() and MyFocus <= 30 then
		if IsSpellAvailable(80483) then
			CastSpellByName(GetSpellInfo(80483),'player')
			return true
		end
	end
	-- Orc Racial
	if IsPlayerSpell(33697) and BurstCD() then
		if IsSpellAvailable(33697) then
			CastSpellByName(GetSpellInfo(33697),'player')
			return true
		end
	end
	-- Troll Racial
	if IsPlayerSpell(26297) and BurstCD() then
		if IsSpellAvailable(26297) then
			CastSpellByName(GetSpellInfo(26297),'player')
			return true
		end
	end
	
	-- Use Mirror of the Blademaster trinket
	if BurstCD() and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		local Trinket1 = select(1,GetInventoryItemID("player", 13))
		local Trinket2 = select(1,GetInventoryItemID("player", 14))
		if Trinket1 == 124224 then UseInventoryItem(13)
		elseif Trinket2 == 124224 then UseInventoryItem(14) end
	end
	
	-- Stampede
	if IsPlayerSpell(121818) then
		if IsSpellAvailable(121818) and BurstCD() and (BLT > 30 or PT > 20 or TBT1 > 5 or TBT2 > 5 or RingT > 5 or (UnitLevel("target") == -1 and TargetHP < 20)) then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(121818),'target')
			return true
		end
	end
	
	-- A Murder of Crows
	if IsPlayerSpell(131894) and RV[1][2] == 1 then
		if IsSpellAvailable(131894) and BurstCD() then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(131894),'target')
			return true
		end
	end
	
	-- Explosive Shot
	if IsSpellAvailable(53301) and (RV[1][2] == 1 or (RV[1][2] == 0 and SS and UnitBuffID("player",168980) and (not IsPlayerSpell(120360) or (IsPlayerSpell(120360) and GetSpellCD(120360) > 1)))) then
		CastSpellByName(GetSpellInfo(53301),'target')
		return true
	end
	
	 -- Barrage
	if IsPlayerSpell(120360) and RV[1][2] == 0 and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(120360) then
			CastSpellByName(GetSpellInfo(120360),'target')
			return true
		end
	end
	
	-- Black Arrow
	if IsSpellAvailable(3674) and not BA and GetSpellCD(53301) > 1 and (RV[1][2] == 1 or (RV[1][2] == 0 and SS)) then
		if not IsPlayerSpell(131894) and IsPlayerSpell(121818) and GetSpellCD(121818) > 120 then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
		end
		CastSpellByName(GetSpellInfo(3674),'target')
		return true
	end
	
	-- single target spells if AoE low numbers of target
	if RV[1][2] == 0 then
	
	-- Explosive Shot
	if IsSpellAvailable(53301) and EnemyCount < 5 then
		CastSpellByName(GetSpellInfo(53301),'target')
		return true
	end
	
	-- A Murder of Crows
	if IsPlayerSpell(131894) then
		if IsSpellAvailable(131894) and BurstCD() then
			if not Blademaster or (Blademaster and Gorefiend) then UseTrinkets() end
			CastSpellByName(GetSpellInfo(131894),'target')
			return true
		end
	end
	
	end
	
	-- Dire Beast
	if IsPlayerSpell(120679) then
		if IsSpellAvailable(120679) and MyFocus <= 85 then
			CastSpellByName(GetSpellInfo(120679),'target')
			return true
		end
	end
	
	-- Multi-Shot while Thrill of the Hunt
	if RV[1][2] == 0 and IsSpellAvailable(2643) and TotH and MyFocus > 50 and SST <= 5
	and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		CastSpellByName(GetSpellInfo(2643),'target')
		return true
	end
	
	-- Serpent Sting to maintain Serpent Sting debuff
	if RV[1][2] == 1 and IsSpellAvailable(3044) and ((not SS or (SS and UnitExists("focus") and not FSS)) or (TotH and MyFocus > 35)) then
		if SST < 5 then
			CastSpellByName(GetSpellInfo(3044),'target')
			return true
		else
			if UnitExists("focus") then
				if not UnitIsDeadOrGhost("focus") and UnitCanAttack("player","focus") then
					if IsSpellInRange(GetSpellInfo(3044), "focus") == 1 and FSST < 5 then
						CastSpellByName(GetSpellInfo(3044),'focus')
						return true
					end
				end
			end
		end
	end
	
	-- maintain Steady Focus buff with double cobra shots
	if IsSpellAvailable(56641) and not IsPlayerSpell(152245) and CobraShotCount == 1
	and (14 + activeRegen*CobraShotCastingTime) <= (UnitPowerMax("player",2) - MyFocus) then 
		CastSpellByName(GetSpellInfo(56641),'target')
		return true
	end
	
	-- Glaive Toss
	if IsPlayerSpell(117050) and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(117050) and ((RV[1][2] == 1 and GetSpellCD(53301) > 0.7) or RV[1][2] == 0) then
			CastSpellByName(GetSpellInfo(117050),'target')
			return true
		end
	end
	-- Powershot
	if IsPlayerSpell(109259) and ((RV[1][2] == 1 and GetSpellCD(53301) > 1) or RV[1][2] == 0)
	and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(109259) then
			CastSpellByName(GetSpellInfo(109259),'target')
			return true
		end
	end
	-- Barrage
	if IsPlayerSpell(120360) and ((RV[1][2] == 1 and GetSpellCD(53301) > 2) or RV[1][2] == 0)
	and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and Gorefiend then
		if IsSpellAvailable(120360) then
			CastSpellByName(GetSpellInfo(120360),'target')
			return true
		end
	end
	
	-- Arcane shot
	if IsPlayerSpell(131894) then -- A Murder of Crows
		if GetSpellCD(131894) < 3 and BurstCD() then FocusSoftCap = 75 else FocusSoftCap = 45 end
	elseif IsPlayerSpell(120360) then -- Barrage
		if GetSpellCD(120360) < 3 and ((RV[1][2] == 0 and SS) or EnemyCount >= 5 or IsPlayerSpell(152245)) then FocusSoftCap = 90 else FocusSoftCap = 45 end
	elseif IsPlayerSpell(152245) then FocusSoftCap = ( 50 - select( 2,GetPowerRegen() ) )
	else FocusSoftCap = 60 end
	
	if RV[1][2] == 1 and IsSpellAvailable(3044) and MyFocus + ExtraFocus >= FocusSoftCap and ((GetSpellCD(53301) > 0.7 and GetSpellCD(3674) > 2) or MyFocus >= 80) then 
		if UnitExists("focus") and not UnitIsDeadOrGhost("focus") and UnitCanAttack("player","focus")
		and IsSpellInRange(GetSpellInfo(3044), "focus") == 1 and FSST < SST then
			CastSpellByName(GetSpellInfo(3044),'focus')
			return true
		else
			CastSpellByName(GetSpellInfo(3044),'target')
			return true
		end
	end
	
	-- Multi-Shot
	if RV[1][2] == 0 and IsSpellAvailable(2643) and Gorefiend 
	and (InDiff ~= "Raid_Mythic" or (InDiff == "Raid_Mythic" and TargetID ~= 78343)) and (MyFocus >= 70 or IsPlayerSpell(152245))
	and (not IsPlayerSpell(109259) or (IsPlayerSpell(109259) and (GetSpellCD(109259) > 1 or not SS)))
	and (not IsPlayerSpell(120360) or (IsPlayerSpell(120360) and (GetSpellCD(120360) > 2 or not SS))) then
		CastSpellByName(GetSpellInfo(2643),'target')
		return true
	end
	
	-- Focus regeneration
	if IsPlayerSpell(152245) then
		-- Focusing Shot
		if IsSpellAvailable(152245) and not IsMoving() then
			CastSpellByName(GetSpellInfo(152245),'target')
			return true
		end
	else
		-- Cobra Shot
		if IsSpellAvailable(56641) then 
			CastSpellByName(GetSpellInfo(56641),'target')
			return true
		end
	end
	
	end
	
end

function WWFHUNTERALLSPECROTATIONSETUP()
	
	if select(2, UnitClass("player")) ~= "HUNTER" then
		print_Log("Error: wrong class!")
		return true
	else
		if UnitLevel("player") < 90 then
			print_Log("Error: Low level!")
			return true
		else
			if GetSpecialization() == nil then
				print_Log("Error: wrong spec - none selected!")
				return true
			elseif GetSpecialization() == 1 then
				Spec = "BM"
				BeastMaster = true
				Marksman = false
				Survival = false
			elseif GetSpecialization() == 2 then
				Spec = "MM"
				BeastMaster = false
				Marksman = true
				Survival = false
			elseif GetSpecialization() == 3 then
				Spec = "SV"
				BeastMaster = false
				Marksman = false
				Survival = true
			end
		end
	end
	
	SLASH_ROTATION1 = "/SwitchRotation"
	function SlashCmdList.ROTATION()
		if RV[1][2] == 0 then
			RV[1][2] = 1
			print(" ")
			print("|CFF1CB619Rotation|R : |CFFFE8A0E"..Spec.." Single Rotation Activated.|R")
		else 
			RV[1][2] = 0
			print(" ")
			print("|CFF1CB619Rotation|R : |CFFFE8A0E"..Spec.." AoE Rotation Activated.|R")
		end
	end
	
	SLASH_BURSTCD1 = "/SwitchBurstCD"
	function SlashCmdList.BURSTCD()
		if BCD[1][2] == 0 then
			BCD[1][2] = 1
			print(" ")
			print("|CFF1CB619Burst CD|R : |CFFFE8A0EBoss only.|R")
		elseif BCD[1][2] == 1 then
			BCD[1][2] = 2
			print(" ")
			print("|CFF1CB619Burst CD|R : |CFFFE8A0EAlways.|R")
		elseif BCD[1][2] == 2 then
			BCD[1][2] = 0
			print(" ")
			print("|CFF1CB619Burst CD|R : |CFFFE8A0EDisabled.|R")
		end
	end
	
	SLASH_TOGGLEMYSPELL1 = "/togglemyspell"
	SlashCmdList["TOGGLEMYSPELL"] = function(msg)
		local Command, SpellName = msg:match("^(%S*)%s*(.-)$")
		if Command == "info" then
			print(" ")
			print("Full list of spells that can be included in rotation:")
			print(" ")
			for i, v in pairs(VFQ) do print(i..") "..GetSpellLink(VFQ[i][3])) end
			print(" ")
			print("Use macro '/togglemyspell localized spell name' to activate these spells for one use.")
			print("Use macro '/togglemyspell CD localized spell name' to activate these spells for using off of CD.")
			print("Use macro '/togglemyspell active' to get info about spells included in rotation.")
			print("Use macro '/SwitchRotation' for switching Single/AoE.")
			print("Use macro '/SwitchBurstCD' for switching burst cooldowns mode: Boss/All/Off.")
		elseif Command == "active" then
			print(" ")
			print("List of spells included in rotation:")
			print(" ")
			x = 0
			for i, v in pairs(VFQ) do if VFQ[i][2] == 1 then x = x + 1 print(x..") "..GetSpellLink(VFQ[i][3])) end end
		elseif Command == "CD" and SpellName ~= "" then
			for i, v in pairs(VFQ) do
				if VFQ[i][1] == SpellName and IsPlayerSpell(VFQ[i][3]) then
					if VFQ[i][2] == 1 then VFQ[i][2] = 0 VFQ[i][5] = 0 else VFQ[i][2] = 1 VFQ[i][5] = 0 end
					print(" ")
					print(string.format("%s has been %s.", GetSpellLink(SpellName), VFQ[i][2] == 1 and "|cff00ff00included|r in rotation for using off of CD" or "|cffff0000excluded|r from rotation"))        
				end
			end
		elseif Command ~= "CD" then
			SpellName = msg
			for i, v in pairs(VFQ) do
				if VFQ[i][1] == SpellName and IsPlayerSpell(VFQ[i][3]) then
					if VFQ[i][2] == 1 then VFQ[i][2] = 0 VFQ[i][5] = 1 else VFQ[i][2] = 1 VFQ[i][5] = 1 end
					print(" ")
					print(string.format("%s has been %s.", GetSpellLink(SpellName), VFQ[i][2] == 1 and "|cff00ff00included|r in rotation for one use" or "|cffff0000excluded|r from rotation"))        
				end
			end
		end
	end

	-- Auto toggle off when used
	function HUNTERATOWU(CheckType, ID, CD, Index)
		if CheckType == "Debuff" then
			if UnitDebuffID("target", ID, "player") then
				if VFQ[Index][2] == 1 and not VFQ[Index][4] then
					VFQ[Index][2] = 0
					VFQ[Index][4] = true
					VFQ[Index][5] = 0
					print(" ")
					print(string.format("%s has been |cffff0000excluded|r from rotation.",GetSpellLink(ID)))
				end
			else
				VFQ[Index][4] = false
			end
		elseif CheckType == "CD" then
			if GetSpellCD(ID) > CD then
				if VFQ[Index][2] == 1 and not VFQ[Index][4] then
					VFQ[Index][2] = 0
					VFQ[Index][4] = true
					VFQ[Index][5] = 0
					print(" ")
					print(string.format("%s has been |cffff0000excluded|r from rotation.",GetSpellLink(ID)))
				end
			else 
				VFQ[Index][4] = false
			end
		elseif CheckType == "RevivePet" then
			if UnitExists("pet") then
				if not UnitIsDead("pet") then
					if VFQ[Index][2] == 1 and not VFQ[Index][4] then
						VFQ[Index][2] = 0
						VFQ[Index][4] = true
						VFQ[Index][5] = 0
						print(" ")
						print(string.format("%s has been |cffff0000excluded|r from rotation.",GetSpellLink(ID)))
					end
				else
					VFQ[Index][4] = false
				end
			end
		end
	end
	
--=============================--
--== Specialisation Settings ==--
--=============================--
	
	-- 1) Localized Spell Name
	-- 2) Spell queue values: 1 - spell in queue, 0 - spell not in queue
	-- 3) Spell ID
	-- 4) HUNTERATOWU check values, all should be 0 or false by default
	-- 5) HUNTERATOWU value: 0 - cast a spell until it is canceled, 1 - cast a spell one time
	
	VFQ = nil
	VFQ = { {GetSpellInfo(19577),   0, 19577,    false, 0}, -- [1] Intimidation
		{GetSpellInfo(19386),   0, 19386,    false, 0}, -- [2] Wyvern Sting
		{GetSpellInfo(109248),  0, 109248,   false, 0}, -- [3] Binding shot
		{GetSpellInfo(982),     0, 982,      false, 0}, -- [4] Revive Pet
		{GetSpellInfo(5116),    0, 5116,     false, 0}, -- [5] Concussive Shot
		{GetSpellInfo(20736),   0, 20736,    false, 0}, -- [6] Distracting Shot
		{GetSpellInfo(13813),   0, 13813,    false, 0}, -- [7] Explosive Trap
		{GetSpellInfo(1499),    0, 1499,     false, 0}, -- [8] Freezing Trap
		{GetSpellInfo(13809),   0, 13809,    false, 0}, -- [9] Ice Trap
		{GetSpellInfo(34600),   0, 34600,    false, 0}, -- [10] Snake Trap
		{GetSpellInfo(5384),    0, 5384,     false, 0}, -- [11] Feign Death
		{GetSpellInfo(19801),   0, 19801,    false, 0}, -- [12] Tranquilizing shot
							     }  -- [13] Racials
	
	-- % of HP for spell use criteria:
	EXHP    =    40    -- Exhilaration
	HSHP    =    40    -- HealthStone
	SMHP    =    80    -- Spirit Mend
	ROSHP	=    80	   -- Roar of Sacrifice
	
	MPHP    =    80    -- Mend Pet
	
	-- indexes:
	Intimidation_Index      =	1
	WyvernSting_Index       =	2
	BindingShot_Index       =	3
	RevivePet_Index         =	4
	ConcussiveShot_Index    =	5
	DistractingShot_Index   =	6
	ExplosiveTrap_Index     =	7
	FreezingTrap_Index      =	8
	IceTrap_Index           =	9
	SnakeTrap_Index         =	10
	FeignDeath_Index        =	11
	TranqShot_Index		=	12
	Racial_Index            =	13
	
	-- Racial abilities:
--	if IsSpellKnown(33697) then table.insert (VFQ, {GetSpellInfo(33697),1,33697,false,0}) end -- Orc Racial
--	if IsSpellKnown(26297) then table.insert (VFQ, {GetSpellInfo(26297),1,26297,false,0}) end -- Troll Racial
--	if IsSpellKnown(80483) then table.insert (VFQ, {GetSpellInfo(80483),1,80483,false,0}) end -- Blood Elf Racial
	
	RV = nil
	RV = { { "Rotation type", 1 } } -- 0 - AoE, 1 - Single
	
	BCD = nil
	BCD = { { "Burst CD mode", 1 } } -- 0 - never, 1 - vs boss only, 2 - always
	
	LastHunterSpec = GetSpecialization()
	
	if GetShapeshiftForm("player") ~= 1 then CastShapeshiftForm(1) end
	
	UIErrorsFrame:UnregisterEvent("UI_ERROR_MESSAGE")
	
	print(" ")
	
	if RV[1][2] == 1 then
		print("|CFF1CB619Rotation|R : |CFFFE8A0E"..Spec.." Single Rotation Activated|R")
	elseif RV[1][2] == 0 then
		print("|CFF1CB619Rotation|R : |CFFFE8A0E"..Spec.." AoE Rotation Activated|R")
	end
	
	print(" ")
	
	if BCD[1][2] == 1 then
		print("|CFF1CB619Burst CD|R : |CFFFE8A0EBoss only.|R")
	elseif BCD[1][2] == 2 then
		print("|CFF1CB619Burst CD|R : |CFFFE8A0EAlways.|R")
	elseif BCD[1][2] == 0 then
		print("|CFF1CB619Burst CD|R : |CFFFE8A0EDisabled.|R")
	end
	
	MyPet = false
	
	WWFHUNTERALLSPECROTATIONPRESETS = true
	
end

function WWFHUNTERALLSPECROTATION()
	
	if not WWFHUNTERALLSPECROTATIONPRESETS then WWFHUNTERALLSPECROTATIONSETUP() return end
	
	if not UnitIsDeadOrGhost("player") then
		
		if IsSpellAvailable(157708) then print("SpellAvailable(157708)") print("157708 cd : "..GetSpellCD(157708)) end
		
		if HunterPause() then return end
		if HunterTrackingFunc() then return end
		
		if HunterBossEvents() then return end
		if HunterSaveAbilities() then return end
		if HunterToggled_Spells() then return end
		
		if      BeastMaster     then BMRotation()
		elseif    Marksman      then MMRotation()
		elseif    Survival      then SVRotation()
		end
		
	end
	
end